﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.LoaiDiemService;
namespace eTMS.Controllers{
public class LoaiDiemControllerBase : CommonController{
	public LoaiDiemControllerBase()
		: base()
	{	
		this.dao = new LoaiDiemServiceClient();
	}
	~LoaiDiemControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private LoaiDiemEntity oLoaiDiem;	
	public LoaiDiemEntity LoaiDiemObject{
		get{
			return oLoaiDiem;
		}
		set{
			oLoaiDiem=value;
		}
	}
	protected LoaiDiemServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSLoaiDiem(false);			
			return _Table;
		}
	}
	public void LoadAllDSLoaiDiem(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oLoaiDiem==null)return false;
		bool result=true;
		try{
			oLoaiDiem = dao.SaveRet(oLoaiDiem);
		}
		catch{
			result=false;
		}
		if(result){
			_ListLoaiDiem.Add(oLoaiDiem);
		}
		return result;
	}
	public bool Update(){
		if(oLoaiDiem==null)return false;
		return dao.Update(oLoaiDiem);
	}
	public bool Delete(){
		if(oLoaiDiem==null)return false;
		bool retvalue = dao.Delete(oLoaiDiem);
		if(retvalue){			
			_ListLoaiDiem.Remove(oLoaiDiem);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 LoaiDiem_ID){
		bool retvalue = dao.DeleteByID(LoaiDiem_ID);		
		if(retvalue){
			LoaiDiemEntity oDeleted=FindLoaiDiem(LoaiDiem_ID);
			if(oDeleted!=null){
				_ListLoaiDiem.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oLoaiDiem = new LoaiDiemEntity();
	}
    //Copy data from data row in grid to object
    public LoaiDiemEntity MapDataRowToObject(DataRow row){
		LoaiDiemEntity o = new LoaiDiemEntity();
		 o.MaLoaiDiem=Convert.ToString(row["MaLoaiDiem"]);
		 o.TenLoaiDiem=Convert.ToString(row["TenLoaiDiem"]);
		 if(!row.IsNull("HeSo"))
			o.HeSo=ConvertNumber(row["HeSo"]);
		 o.GhiChu=Convert.ToString(row["GhiChu"]);
		 if(!row.IsNull("LoaiDiem_ID"))
             o.LoaiDiem_ID = (Int32)(row["LoaiDiem_ID"]);
         if (!row.IsNull("LoaiDiem_ID"))
             o.IsDiemTongKet = ConvertBool(row["IsDiemTongKet"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(LoaiDiemEntity o,DataRow row){
		row["MaLoaiDiem"]=o.MaLoaiDiem;
		row["TenLoaiDiem"]=o.TenLoaiDiem;
		row["HeSo"]=o.HeSo;
		row["GhiChu"]=o.GhiChu;
        row["LoaiDiem_ID"] = o.LoaiDiem_ID;
        row["IsDiemTongKet"] = o.IsDiemTongKet;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.LoaiDiemObject,row);
    }
	public void LoadObject(Int32 LoaiDiem_ID){
		LoaiDiemEntity oLoad=FindLoaiDiem(LoaiDiem_ID);
		if(oLoad==null){
			oLoaiDiem=this.LoadLoaiDiem(LoaiDiem_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oLoaiDiem.IsNullLoaiDiem_ID;
			if(!bNotIsNull){
				this.ListLoaiDiem.Add(oLoaiDiem);
			}
		}
		else{
			oLoaiDiem=oLoad;
		}
	}
	#region CachedList LoaiDiem
	private static List<LoaiDiemEntity> _ListLoaiDiem=new List<LoaiDiemEntity>();
    public List<LoaiDiemEntity>ListLoaiDiem{
		get{
			return _ListLoaiDiem;
		}
	}
	public static List<LoaiDiemEntity>ListLoaiDiemStatic{
		get{
			return _ListLoaiDiem;
		}
	}
	public LoaiDiemEntity FindLoaiDiem(Int32 LoaiDiem_ID){
		LoaiDiemEntity oResult=this.ListLoaiDiem.Find(delegate(LoaiDiemEntity oFind){
			bool result=true; 
			result&=oFind.LoaiDiem_ID.Equals(LoaiDiem_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllLoaiDiem(bool clearCache){
		if(_ListLoaiDiem.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		LoaiDiemServiceClient dao = new LoaiDiemServiceClient();
		_ListLoaiDiem=new List<LoaiDiemEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public LoaiDiemEntity LoadLoaiDiem(Int32 LoaiDiem_ID){			
		LoaiDiemEntity o=dao.SelectOne(LoaiDiem_ID);		
		return o;
	}
	#endregion
}
}
